package by.pavlyukevich.paymentsystem.dataaccess.erip.impl;

import java.util.List;

import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.SetJoin;

import org.springframework.stereotype.Repository;

import by.pavlyukevich.paymentsystem.dataaccess.erip.EripNameDao;
import by.pavlyukevich.paymentsystem.dataaccess.impl.AbstractDaoImpl;
import by.pavlyukevich.paymentsystem.datamodel.erip.EripName;
import by.pavlyukevich.paymentsystem.datamodel.payment.PaymentDetail;
import by.pavlyukevich.paymentsystem.datamodel.payment.PaymentDetail_;

@Repository
public class EripNameDaoImpl extends AbstractDaoImpl<Integer, EripName> implements EripNameDao {

	@Override
	public List<EripName> getEripsByPayment(PaymentDetail paymentDetail) {
		CriteriaBuilder cBuilder = getEntityManager().getCriteriaBuilder();

		CriteriaQuery<EripName> root = cBuilder.createQuery(EripName.class);
		Root<PaymentDetail> criteria = root.from(PaymentDetail.class);

		SetJoin<PaymentDetail, EripName> joinPaymentDetail = criteria
				.join(PaymentDetail_.eripNames);

		root.select(joinPaymentDetail).where(
				cBuilder.equal(criteria.get(PaymentDetail_.id), paymentDetail.getId()));

		TypedQuery<EripName> query = getEntityManager().createQuery(root);
		return query.getResultList();
	}

	protected EripNameDaoImpl() {
		super(EripName.class);
	}
}
